package com.wisdytech.linkdcs.linkdcsservicepad.pad.action;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sun.nio.sctp.HandlerResult;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.linkdcs.linkdcsservicepad.common.BaseUtils;
import com.wisdytech.linkdcs.linkdcsservicepad.pad.service.IpqcInspectionSheetService;
import com.wisdytech.linkdcs.pad.model.IpqcInspectionActual;
import com.wisdytech.linkdcs.pad.model.IpqcInspectionDetail;
import com.wisdytech.linkdcs.pad.model.IpqcInspectionRecord;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-08-13
 */
@Controller
@RequestMapping("/app/ipqcInspectionSheet")
public class IpqcInspectionSheetAction {

    @Autowired
    private IpqcInspectionSheetService ipqcInspectionSheetService;

    /**
     *扫码登记检测
     * @param request
     * @param response
     * @return 返回结果集
     */
    @RequestMapping(value = "/confirmInspectionTarget.mvc")
    @ResponseBody
    public StandardResult confirmInspectionTarget(HttpServletRequest request, HttpServletResponse response){
//        queryParams: {"inspectionCategory":"1","inputMethod":"1","inspectionTargetCode":"LN1534128814947","username":"chenbin"}
//        inspectionSheetId:
        Map<String, String> paramsMap = (Map<String, String>) JSON.parse(request.getParameter("queryParams"));
        String inspectionSheetId = request.getParameter("inspectionSheetId");

        Map<String, Object> map = this.ipqcInspectionSheetService.
                confirmInspectionTarget(paramsMap,inspectionSheetId);
        return StandardResult.getSuccessResult(map);
    }

    /**
     *通过检验单id-获取检测对象任务
     * @param request
     * @param response
     * @return 返回结果集
     */
    @RequestMapping(value = "/getIpqcDetail.mvc")
    @ResponseBody
    public StandardResult getIpqcDetail(HttpServletRequest request, HttpServletResponse response){
        String inspectionSheetId = request.getParameter("inspectionSheetId");
        List<IpqcInspectionDetail> list = this.ipqcInspectionSheetService.getIpqcDetail(inspectionSheetId);
        return StandardResult.getSuccessResult(list);
    }

    /**
     *通过检验对象明细id-检验对象明细生成检验项
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/checkIpqcDetail.mvc")
    @ResponseBody
    public StandardResult checkIpqcDetail(HttpServletRequest request, HttpServletResponse response){
        String inspectionDetailId = request.getParameter("inspectionDetailId");
        String username = request.getParameter("username");
        this.ipqcInspectionSheetService.checkIpqcDetail(inspectionDetailId,username);
       return StandardResult.getSuccessResult();
    }

    /**
     *通过检验明细id获取检验项
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getIpqcRecord.mvc")
    @ResponseBody
    public StandardResult getIpqcRecord(HttpServletRequest request, HttpServletResponse response){
        String inspectionDetailId = request.getParameter("inspectionDetailId");
        String itemType = request.getParameter("itemType");
        List<IpqcInspectionRecord> list = this.ipqcInspectionSheetService.getIpqcRecord(inspectionDetailId,itemType);
        return StandardResult.getSuccessResult(list);
    }

    /**
     *通过检验记录id获取检验项实测值
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getInspectionActual.mvc")
    @ResponseBody
    public StandardResult getInspectionActual(HttpServletRequest request, HttpServletResponse response){
        String inspectionRecordId = request.getParameter("inspectionRecordId");
        List<IpqcInspectionActual> list = this.ipqcInspectionSheetService.getInspectionActual(inspectionRecordId);
        return StandardResult.getSuccessResult(list);
    }

    /**
     *保存实测值对象
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveInspectionActual.mvc")
    @ResponseBody
    public StandardResult saveInspectionActual(HttpServletRequest request, HttpServletResponse response){
        //storeData: [{"inspectionRecordId":"4028924e65379b64016537e4bfb703be","id":"extModel959-2","inspectionResult":"1","actualValue":20},{"inspectionRecordId":"4028924e65379b64016537e4bfb703be","id":"extModel959-1","inspectionResult":"1","actualValue":10}]
        String storeData = request.getParameter("storeData");
        String username = request.getParameter("username");
        List<IpqcInspectionActual> list = JSONArray.parseArray(storeData, IpqcInspectionActual.class);
        this.ipqcInspectionSheetService.saveInspectionActual(list,username);
        return StandardResult.getSuccessResult();
    }

    /**
     * 删除实测值对象
     * @param ids
     */
    @RequestMapping(value = "/deleteInspectionActual.mvc")
    @ResponseBody
    public StandardResult deleteInspectionActual(String ids){
        this.ipqcInspectionSheetService.deleteInspectionActual(ids);
        return StandardResult.getSuccessResult();
    }

    /**
     *保存或修改检验记录对象
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveIpqcRecord.mvc")
    @ResponseBody
    public StandardResult saveIpqcRecord(HttpServletRequest request, HttpServletResponse response){
        String storeData = request.getParameter("storeData");
        List<IpqcInspectionRecord> list = JSONArray.parseArray(storeData, IpqcInspectionRecord.class);
        this.ipqcInspectionSheetService.saveIpqcRecord(list);
        return StandardResult.getSuccessResult();
    }

    /**
     *保存检验明细检验结果
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateIpqcDetail.mvc")
    @ResponseBody
    public StandardResult updateIpqcDetail(HttpServletRequest request, HttpServletResponse response){
        String ipqcDetail = request.getParameter("ipqcDetail");
        IpqcInspectionDetail det = JSONArray.parseObject(ipqcDetail, IpqcInspectionDetail.class);
        this.ipqcInspectionSheetService.updateIpqcDetail(det);
        return StandardResult.getSuccessResult();
    }

}

